use dta/SMDLevel.dta, clear

egen NoP1=rowtotal(run1_s-run1_nkp)
egen NoP2=rowtotal(run2_s-run2_nkp)

****************************************************************************************
****************** CREATING DISTRICT-WIDE RUN ******************************************
****************************************************************************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen run1_district_`party'=0
egen temp_`party'=sum(run1_`party'), by(PR year)
replace run1_district_`party'=1 if temp_`party'>0 & temp_`party'!=.
}
drop temp*
egen NoP1_district=rowtotal(run1_district_s-run1_district_nkp)
replace NoP1=NoP1_district if year>1920 /* this line replaces 11 cases where a party receives zero votes, but is running district-wide, e.g. "fiskerne og det liberale folkeparti" received zero votes in 1604 */

****************************************************************************************

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

gen NoPF=NoP1
replace NoPF=NoP2 if SECOND==1

**************** NUMBER OF PARTIES *********************************
***** What is the change in the NoP around the reform?

xtset SMD year
gen Next_NoP1=f3.NoP1
gen diff_NoP1=Next_NoP1-NoP1
sum diff_NoP1 if year==1918
sort diff_NoP1
li SMD PR NoP1 Next_NoP1 diff_NoP1 if year==1918

**************** NUMBER OF PARTIES *********************************
/*
foreach var in NoP1 NoPF {
gen zz=1
bysort `var' year: egen temp=sum(zz)
gen s=temp/91
scatter `var' year [fw=s], msymbol(Oh) yscale(range(0 6.5)) ylabel(1(1)6) xscale(range(1908 1928)) xlabel(1909(3)1927)  xtitle("") ytitle("") plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s1mono) legend(off)
graph save figures/gph/`var'.gph, replace
graph export figures/`var'.pdf, replace
drop zz temp s
}
*/

**************** ENOP FIRST ROUND *********************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vs1SQ_`party'=vs1_`party'*vs1_`party'
}
egen temp=rowtotal(vs1SQ_s-vs1SQ_nkp)
gen ENoP1=1/temp
drop temp
*graph box ENoP1, over(year) ytitle("") graphregion(fcolor(white)) scheme(s1mono) legend(off) yscale(range(0 6.5)) ylabel(1(1)6)
*graph export figures/ENoP1.pdf, replace
*graph save figures/gph/ENoP1.gph, replace


**************** ENOP FINAL ROUND *********************************

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vsF_`party'=vs1_`party'
replace vsF_`party'=vs2_`party' if SECOND==1
}

foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen vsFSQ_`party'=vsF_`party'*vsF_`party'
}
egen temp=rowtotal(vsFSQ_s-vsFSQ_nkp)
gen ENoPF=1/temp
drop temp


**************** both enop and nop in same figure ---- split by M *********************************
preserve
egen PR_M=max(M), by(SMD)

forvalues i=3(1)8{

	collapse NoP1 ENoP1 ENoPF, by(PR_M year)
	tw (scatter NoP1 year if PR_M==`i' & year>1920, connect(l) lpattern(dash) msymbol(circle) mcolor(black) mlcolor(black)) ///
	(scatter ENoP1 year if PR_M==`i' & year>1920, connect(l) lpattern(dash) msymbol(circle) mcolor(white) mlcolor(black)) ///
	(scatter ENoPF year if PR_M==`i' & year>1920, connect(l) lpattern(dash) msymbol(circle) mcolor(gray) mlcolor(black)) ///
	(scatter NoP1 year if PR_M==`i' & year<1920, connect(l) lpattern(dash) msymbol(circle) mcolor(black) mlcolor(black))  ///
	(scatter ENoP1 year if PR_M==`i' & year<1920, connect(l) lpattern(dash) msymbol(circle) mcolor(white) mlcolor(black))  ///
	(scatter ENoPF year if PR_M==`i' & year<1920, connect(l) lpattern(dash) msymbol(circle) mcolor(gray) mlcolor(black))  ///
	, title(Magnitude `i') yscale(range(1 7)) ylabel(1(1)7) xlabel(1909(3)1927)  xtitle("") ytitle("") plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s1mono) legend(order(1 "NoP first round" 2 "ENoP first round" 3 "ENoP final round")) legend(cols(3))
	graph save figures/gph/M`i'.gph, replace
	}

restore

cd figures/gph
grc1leg M3.gph M4.gph M5.gph M6.gph M7.gph M8.gph , graphregion(fcolor(white)) scheme(s1mono) 
graph export ../FigureA2.pdf, replace
cd ../../